from print_matrix import print_matrix


def rotate(matrix):
    if not matrix:
        raise Exception('非法输入')
    n = len(matrix)
    for layer in range(0, (n + 1) // 2):
        first = layer
        last = n - first - 1
        for i in range(last - first):
            # 保存上
            tmp = matrix[first][first + i]
            # 左移到上
            matrix[first][first + i] = matrix[last - i][first]
            # 下移到左
            matrix[last - i][first] = matrix[last][last - i]
            # 右移到下
            matrix[last][last - i] = matrix[first + i][last]
            # 上移到右
            matrix[first + i][last] = tmp
    return matrix


matrix1 = [[1, 2, 3, 4, 91],
           [5, 0, 7, 8, 92],
           [9, 10, 11, 12, 93],
           [13, 14, 15, 0, 94],
           [17, 18, 19, 20, 95]]

print_matrix(matrix1)
rotate(matrix1)
print_matrix(matrix1)

matrix2 = [[5]]
rotate(matrix2)
print_matrix(matrix2)
